package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.orewar.UserOreWarHp;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserOreWarHpDaoImpl extends TempDataObjectDao<UserOreWarHp> {

    public Map<Long, UserOreWarHp> getUserOreWarHpMap() {
        Map<Long, UserOreWarHp> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_orewar_hp";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserOreWarHp data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public UserOreWarHp getUserOreWarHp(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_orewar_hp where userId=? AND activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setLong(2, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserOreWarHp data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserOreWarHp data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_activity_orewar_hp(userId,activityId,hp,lastUpdateTime,todayUpdate) VALUES (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getActivityId());
            call.setInt(i++, data.getHp());
            call.setLong(i++, data.getLastUpdateTime());
            call.setLong(i++, data.getTodayUpdate());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserOreWarHp data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_activity_orewar_hp SET hp=?,lastUpdateTime=?,todayUpdate=? WHERE userId=? AND activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getHp());
            call.setLong(i++, data.getLastUpdateTime());
            call.setLong(i++, data.getTodayUpdate());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getActivityId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserOreWarHp parseData(ResultSet rs) throws SQLException {
        UserOreWarHp data = new UserOreWarHp();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setHp(rs.getInt("hp"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setTodayUpdate(rs.getLong("todayUpdate"));
        return data;
    }
}
